package com.huifaapp.mall.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.tmall_teamoneFragmentAdapter;
import com.huifaapp.mall.adapter.tmall_teamtwoFragmentAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.bean.TeamBean;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by duanyu on 2018/1/5.
 */

public class tmall_teamtwoFragnet extends Fragment{

    private View inflate;
    private XRecyclerView tmall_teamtwoRecyclerView;
    private EditText tmall_teamtwo_editText_userName;
    private EditText tmall_teamtwo_editText_userPhone;
    private EditText tmall_teamtwo_editText_startTime;
    private EditText tmall_teamtwo_editText_endTime;
    private Button teamtwo_inquire;
    private com.huifaapp.mall.adapter.tmall_teamtwoFragmentAdapter tmall_teamtwoFragmentAdapter;
    private   int curr;
    private List<TeamBean.DataBean.ListBean> teamBeanlist1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragnet_tmall_teamtwo, container, false);
                    initView();
                    initHttp();
        teamtwo_inquire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initTeamTwoHttp();
                tmall_teamtwoFragmentAdapter.notifyDataSetChanged();
            }
        });

        return inflate;
    }

    private void initTeamTwoHttp() {
        Log.e("我的团队-二级好友",tmall_teamoneFragment.TEAMURL+"?token="+SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)
                +"&appId="+AUniqueIdentifier.getDeviceId(getActivity())+"&level=two"+"&start="+curr+"&length="+"10"+"&userName="+
                tmall_teamtwo_editText_userName.getText().toString()+"&userPhone="+tmall_teamtwo_editText_userPhone.getText().toString()
        +"&startTime="+tmall_teamtwo_editText_startTime.getText().toString()+"&endTime="+tmall_teamtwo_editText_endTime.getText().toString());
        Map<String,String> TwoTeamteammap=new HashMap<>();
        TwoTeamteammap.put("token",SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        TwoTeamteammap.put("appId",AUniqueIdentifier.getDeviceId(getActivity()));
        TwoTeamteammap.put("level","two");
        TwoTeamteammap.put("start",curr+"");
        TwoTeamteammap.put("length","10");
        TwoTeamteammap.put("userName",tmall_teamtwo_editText_userName.getText().toString());
        TwoTeamteammap.put("userPhone",tmall_teamtwo_editText_userPhone.getText().toString());
        TwoTeamteammap.put("startTime",tmall_teamtwo_editText_startTime.getText().toString());
        TwoTeamteammap.put("endTime",tmall_teamtwo_editText_endTime.getText().toString());
        OkHttpUtils.post().params(TwoTeamteammap).url(tmall_teamoneFragment.TEAMURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                tmall_teamtwoRecyclerView.refreshComplete();
//                Gson gson=new Gson();
//                teamBeanlist1.clear();
//                TeamBean teaminquireBean = gson.fromJson(response, TeamBean.class);
                initData(response);
//                if (teaminquireBean.equals("10000")){
//                    teamBeanlist1.addAll(teaminquireBean.getData().getList());
//                    tmall_teamtwoFragmentAdapter.notifyDataSetChanged();
//                }


            }
        });
    }

    private void initHttp() {
        Map<String,String>Twoteammap=new HashMap<>();
        Twoteammap.put("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN));
        Twoteammap.put("appId", AUniqueIdentifier.getDeviceId(getActivity()));
        Twoteammap.put("level","two");
        Twoteammap.put("start",curr+"");
        Twoteammap.put("length","10");
        OkHttpUtils.post().params(Twoteammap).url(tmall_teamoneFragment.TEAMURL).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                
            }

            @Override
            public void onResponse(String response) {
                Log.e("wozhejiush",response);
                tmall_teamtwoRecyclerView.refreshComplete();
                initData(response);

            }
        });
    }

    private void initData(String response) {
        Gson gson = new Gson();
        TeamBean teamBean = gson.fromJson(response, TeamBean.class);
        if (teamBean.getCode().equals("10000")) {
            if (teamBean != null && teamBean.getData() != null && teamBean.getData().getList() != null) {
                if (curr == 0) {
                    teamBeanlist1.clear();
                }
                teamBeanlist1.addAll(teamBean.getData().getList());
                tmall_teamtwoFragmentAdapter = new tmall_teamtwoFragmentAdapter(teamBeanlist1, getActivity());
                tmall_teamtwoRecyclerView.setAdapter(tmall_teamtwoFragmentAdapter);
                tmall_teamtwoFragmentAdapter.notifyDataSetChanged();

            }
        }
    }
    private void initView() {
        teamBeanlist1 = new ArrayList<>();
        tmall_teamtwoRecyclerView = (XRecyclerView) inflate.findViewById(R.id.tmall_teamtwoRecyclerView);
        tmall_teamtwoRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        tmall_teamtwoRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                curr = 0;
                initHttp();
            }

            @Override
            public void onLoadMore() {
                curr ++;
                initHttp();
            }
        });

        initHttp();
        tmall_teamtwo_editText_userName = (EditText) inflate.findViewById(R.id.tmall_teamtwo_EditText_userName);
        tmall_teamtwo_editText_userPhone = (EditText) inflate.findViewById(R.id.tmall_teamtwo_editText_userPhone);
        tmall_teamtwo_editText_startTime = (EditText) inflate.findViewById(R.id.tmall_teamtwo_editText_startTime);
        tmall_teamtwo_editText_endTime = (EditText) inflate.findViewById(R.id.tmall_teamtwo_editText_endTime);
        teamtwo_inquire = (Button) inflate.findViewById(R.id.teamtwo_inquire);
    }
}
